package pos;

import java.awt.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.media.j3d.*;
import javax.swing.*;
import pos.utils.*;


/**
 * 应用程序的窗口，3D绘图环境的容器
 * @author  plus
 */
public class GUIEntrance extends JFrame {

    /**
     * 根据配置构造窗口
     * @param profile 指导构造窗口的配置
     */
    public GUIEntrance(Profile profile) {
        initComponents(profile);
        this.setVisible(true);
    }
    /**
     * 更改当前显示模式，但无法改变全屏设置
     * @param mode 新的显示模式，其中分辨率和色彩设置将被应用，全屏设置将被保存，下次启动后应用
     */
    public void ChangeDisplayMode(DisplayMode mode){
        if (isFullScreen){
            resizeFullScreenWindow(mode);
        }else{
            resizeWindowedWindow(mode);
        }
    }

    /**
     * 向窗口添加3D绘图环境，同时移去初始化画面
     * @param comp 需要添加的3D绘图环境
     * @return 已舍弃，永远返回null
     */
    public Component add(Canvas3D comp) {
        if(PanelLoading!=null) {
            PanelLoading.setVisible(false);
            this.remove(PanelLoading);
            this.PanelLoading=null;
        }
        super.add(comp);
        return null;
    }

    
    private void initComponents(Profile profile) {

        setFocusTraversalPolicyProvider(true);
        setResizable(false);
        StringRes.languageChangeTo(StringRes.ZHCN);
        setTitle(StringRes.getString("AppTitle"));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        if (profile.isFullScreen()){
            initFullScreenWindow(profile.getMode());
        }else{
            initWindowedWindow(profile.getMode());
        }
        this.setLayout(new BorderLayout());
        
        this.add(PanelLoading);
    }                    

    private void initWindowedWindow(DisplayMode mode){
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-mode.getWidth())/2, (screenSize.height-mode.getHeight())/2,
                mode.getWidth(), mode.getHeight());
        isFullScreen=false;
        width=mode.getWidth();
        height=mode.getHeight();
    }
    
    private void initFullScreenWindow(DisplayMode mode){
        this.setUndecorated(true);                                              //全屏模式下不显示边框
        GraphicsEnvironment environment=GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device=environment.getDefaultScreenDevice();
        device.setFullScreenWindow(this);
        device.setDisplayMode(mode);
        isFullScreen=true;
        width=mode.getWidth();
        height=mode.getHeight();
    }
    private void resizeWindowedWindow(DisplayMode mode){
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-mode.getWidth())/2, (screenSize.height-mode.getHeight())/2,
                mode.getWidth(), mode.getHeight());
        width=mode.getWidth();
        height=mode.getHeight();
    }
    
    private void resizeFullScreenWindow(DisplayMode mode){
        GraphicsEnvironment environment=GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice device=environment.getDefaultScreenDevice();
        device.setDisplayMode(mode);
        width=mode.getWidth();
        height=mode.getHeight();
    }
    
    
    private void exitForm(java.awt.event.WindowEvent evt) {                          
        System.exit(0);
    }                         

    
    private JPanel PanelLoading = new JPanel() {
        private BufferedImage imgLoadingLogo=null;
        protected void paintComponent(Graphics g) {
            if (imgLoadingLogo==null){
                try{
                    imgLoadingLogo=ImageIO.read(new File(PathRes.getPath("Image.Loading")));
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            if (isFullScreen)
                g.drawImage(imgLoadingLogo, 0, 0, width, height, this);
            else
                g.drawImage(imgLoadingLogo, 0, 0, width, height-15, this);
        }
    };     
    
    private boolean isFullScreen;
    public int width, height;
}

